#include "menuWidget.h"

menuWidget::menuWidget(QWidget *parent) : QWidget(parent)
{
    cameraBtn = new MenuItemBtn();
    showBtn = new MenuItemBtn();
    settingBtn = new MenuItemBtn();
    //用作空白处理
    Space = new QWidget();
    VLayout = new QVBoxLayout(this);

    VLayout->addWidget(cameraBtn);
    VLayout->setAlignment(cameraBtn, Qt::AlignHCenter);
    VLayout->addWidget(showBtn);
    VLayout->setAlignment(showBtn, Qt::AlignHCenter);
    VLayout->addWidget(Space);
    VLayout->addWidget(settingBtn);
    VLayout->setAlignment(settingBtn, Qt::AlignHCenter);
    VLayout->setMargin(0);

    //将按钮都添加到链表中方便后面进行统一处理
    Btns.append(std::make_pair(cameraBtn, menuKind::Camera));
    Btns.append(std::make_pair(showBtn, menuKind::Show));
    Btns.append(std::make_pair(settingBtn, menuKind::Setting));

    this->setLayout(VLayout);
    this->setBtnStyleSheet(QSS::QPushButton);

    cameraBtn->setBtnIcon(":/WYJClose.png", btnIconKind::General);
    cameraBtn->setBtnIcon(":/WYJOpne.png", btnIconKind::Clicked);

    showBtn->setBtnIcon(":/showClose.png", btnIconKind::General);
    showBtn->setBtnIcon(":/showOpen.png", btnIconKind::Clicked);

    settingBtn->setBtnIcon(":/settingClose.png", btnIconKind::General);
    settingBtn->setBtnIcon(":/settingOpen.png", btnIconKind::Clicked);

    //将菜单项中按钮点击信号与槽函数进行绑定
    connect(cameraBtn, SIGNAL(myCliecked(MenuItemBtn*)), this, SLOT(ClickedBtn(MenuItemBtn*)));
    connect(showBtn, SIGNAL(myCliecked(MenuItemBtn*)), this, SLOT(ClickedBtn(MenuItemBtn*)));
    connect(settingBtn, SIGNAL(myCliecked(MenuItemBtn*)), this, SLOT(ClickedBtn(MenuItemBtn*)));
    this->setFixedWidth(80);
}

bool menuWidget::setBtnStyleSheet(const QString &qss)
{
    if(qss.isEmpty())
    {
        DLOG("传入qss样式为空");
        return false;
    }
    for(auto e : Btns)
    {
        e.first->setBtnStyleSheet(qss);
    }
    return true;
}

bool menuWidget::setBackgroundColorFormPalette(const QColor& color)
{
    QPalette palette;
    palette.setBrush(QPalette::Background, color);
    this->setPalette(palette);
    this->setAutoFillBackground(true);
    return true;
}

void menuWidget::SettingBtnClose()
{
    settingBtn->setBtnStatus(btnStatus::Close);
}

void menuWidget::ClickedBtn(MenuItemBtn* Btn)
{
    if(Btn == currentBtn) return ;
    if(Btn == settingBtn)
    {
        emit menuBtnClicked(menuKind::Setting);
        return ;
    }
    currentBtn = Btn;       // 设置按钮不与其他两个按钮共用逻辑
    for(auto e : Btns)
    {
        if(e.first != currentBtn) {
            e.first->setBtnStatus(btnStatus::Close);
        }else
        {
            currentBtnKind = e.second;
        }
    }
    emit menuBtnClicked((int)currentBtnKind);
}
